package com.shunya.diskorganizer.io;

import com.shunya.diskorganizer.model.FileMetaData;
import com.shunya.diskorganizer.model.builder.FileMetaDataBuilder;
import com.shunya.diskorganizer.tinytypes.Name;
import com.shunya.diskorganizer.tinytypes.Owner;
import com.shunya.diskorganizer.tinytypes.Size;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

import static java.nio.file.Files.*;
import static java.nio.file.LinkOption.NOFOLLOW_LINKS;

public class FileMetaDataReader {

    public FileMetaData getMetaData(File inputFile) throws IOException, FileMetaDataReaderException {
        Path path = inputFile.toPath();
        if (notExists(path)) throw new FileMetaDataReaderException("Invalid file specified.");
        return new FileMetaDataBuilder().
                with(new Name(path.getName(path.getNameCount() - 1).toString())).
                with(new Size(size(path))).
                with(new Owner(getOwner(path, NOFOLLOW_LINKS).getName())).build();
    }
}
